package com.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hdb.DB;
import com.hdb.TNotice;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;

@ParentPackage("default")
@Namespace("/notice")
@Results({
        @Result(location = "/admin/notice/noticeManage.jsp"),
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "edit", location = "/admin/notice/noticeEdit.jsp"),
        @Result(name = "noticeAll", location = "/view/notice/noticeAll.jsp"),
        @Result(name = "noticeEditPage", location = "/admin/notice/noticeEdit.jsp")
})
@InterceptorRef("adminStack")/*设置过滤器*/
public class AdminNoticeAction extends BasicAction {
    private int noticeId;
    private String noticeTitle;
    private String noticeInfo;
    private int noticeTop;
    private DB db;
    private String message;
    private String path;

    /*
     * 管理员查看发布的公告
     */
    @Action("noticeManage")
    public String noticeManage() {
        int[] p = getP();
        List noticeList = db.noticeDAO.join(db.adminDAO, "adminId", p[0], p[1]);
        getRequest().put("noticeList", noticeList);
        getPLink(noticeList.size());
        return ActionSupport.SUCCESS;
    }

    /*
    * 删除公告
    */
    @Action("noticeDel")
    public String noticeDel() {
        TNotice notice = db.noticeDAO.findById(noticeId);
        this.setMessage(getResultInfo(
                db.noticeDAO.delete(notice)
        ));
        this.setPath("notice/noticeManage.action");
        return "succeed";
    }

    /*修改公告*/
    @Action("noticeEdit")
    public String noticeEdit() {
        TNotice notice = db.noticeDAO.findById(noticeId);
        notice.setNoticeTitle(noticeTitle);
        notice.setNoticeInfo(noticeInfo);
        notice.setNoticeTop(noticeTop);
        db.noticeDAO.attach(notice);
        this.setPath("notice/noticeManage.action");
        return "succeed";
    }
    @Action("noticeEditPage")
    public String noticeEditPage() {
        TNotice notice = db.noticeDAO.findById(noticeId);
        getRequest().put("notice",notice);
        return "noticeEditPage";
    }

    /*
     * 添加新的公告
     */
    @Action("noticeAdd")
    public String noticeAdd() {
        TNotice notice = new TNotice();
        notice.setAdminId(getAdmin().getAdminId());
        notice.setNoticeInfo(noticeInfo);
        notice.setNoticeTitle(noticeTitle);
        notice.setNoticeDate(new Timestamp(new Date().getTime()));
        notice.setNoticeTop(noticeTop);
        this.setMessage(getResultInfo(
                db.noticeDAO.save(notice)
        ));
        this.setPath("notice/noticeManage.action");
        return "succeed";
    }

    public int getNoticeId() {
        return noticeId;
    }

    public void setNoticeId(int noticeId) {
        this.noticeId = noticeId;
    }

    public String getNoticeTitle() {
        return noticeTitle;
    }

    public void setNoticeTitle(String noticeTitle) {
        this.noticeTitle = noticeTitle;
    }

    public String getNoticeInfo() {
        return noticeInfo;
    }

    public void setNoticeInfo(String noticeInfo) {
        this.noticeInfo = noticeInfo;
    }

    public int getNoticeTop() {
        return noticeTop;
    }

    public void setNoticeTop(int noticeTop) {
        this.noticeTop = noticeTop;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}
